package lab02.test23;

import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class Test23 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入你的生日信息，格式形如：2017-10-31:");

        Calendar calendar = Calendar.getInstance();
        String string = scanner.nextLine();

        //String.subString(start,end)截取的字符串包括起点所在的字符串，不包括终点所在的字符串
        String year = string.substring(0, 4);
        String month = string.substring(5, 7);
        String day = string.substring(8);

        //calendar.set设置日历字段中的值：YEAR,MONTH,DAY
        //Integer.parseInt() 返回的是一个int的值。
        calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));

        System.out.print("你的生日在:");

        int week = calendar.get(calendar.DAY_OF_WEEK);
        switch (week) {
            case 1:
                System.out.println("星期天");
                break;
            case 2:
                System.out.println("星期一");
                break;
            case 3:
                System.out.println("星期二");
                break;
            case 4:
                System.out.println("星期三");
                break;
            case 5:
                System.out.println("星期四");
                break;
            case 6:
                System.out.println("星期五");
                break;
            default:
                System.out.println("星期六");
                break;
        }
        Date date2 = new Date();
        calendar.set(2017, Integer.parseInt(month) - 1, Integer.parseInt(day));
        Date date1 = calendar.getTime();//将日期转成毫秒数
        //date1.getTime() - date2.getTime()获取两个日期代表的毫秒数，相减以后获取两个日差的毫秒数，
        long birthday = (date1.getTime() - date2.getTime() + 1000000) / (24 * 60 * 60 * 1000);
        if (birthday < 0) {
            birthday = (date2.getTime() - date1.getTime() + 1000000) / (24 * 60 * 60 * 1000);
            System.out.println("今年你的生日已过" + birthday + "天");
        } else
            System.out.println("距离你的生日还有" + birthday + "天。");
    }
}
